package com.xiyu.service.service.files;

import com.xiyu.service.vo.files.landOwnership.LandOwnershipSingleGetOutput;
import com.xiyu.service.vo.files.landOwnership.LandOwnershipUpdateInput;
import com.xiyu.service.vo.files.landOwnership.LandOwnershipCreateInput;
import com.xiyu.service.model.files.certificate.FilesLandOwnership;
import com.xiyu.service.repository.files.certificate.FilesLandOwnershipRepository;
import com.xiyu.service.vo.files.landOwnership.LandOwnershipPageQueryOutput;
import com.xiyu.service.vo.files.landOwnership.LandOwnershipPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.files.LandOwnershipConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.files.LandOwnershipErrorCode.*;

/**
 * 土地使用权证 Service 实现类
 */
@Service
@Validated
public class LandOwnershipServiceImpl implements LandOwnershipService {

    @Resource
    private FilesLandOwnershipRepository filesLandOwnershipRepository;




    @Override
    public PageResult<LandOwnershipPageQueryOutput> pageQuery(LandOwnershipPageQueryInput inputVO) {
        Page<FilesLandOwnership> pageFilesLandOwnership = filesLandOwnershipRepository.pageQuery(inputVO);
        List<LandOwnershipPageQueryOutput> listFilesLandOwnership = LandOwnershipConvert.INSTANCE.pageQueryOutputConvert(pageFilesLandOwnership.getRows());
        return new PageResult<>(listFilesLandOwnership, pageFilesLandOwnership.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(LandOwnershipCreateInput inputVO) {
        FilesLandOwnership newFilesLandOwnership = LandOwnershipConvert.INSTANCE.createInputConvert(inputVO);
        newFilesLandOwnership = filesLandOwnershipRepository.insert(newFilesLandOwnership);
        return newFilesLandOwnership.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(LandOwnershipUpdateInput inputVO) {
        Optional<FilesLandOwnership> optionalOldFilesLandOwnership = filesLandOwnershipRepository.findById(inputVO.getId());
        if(!optionalOldFilesLandOwnership.isPresent()){
            throw exception(FILES_LAND_OWNERSHIP_NOT_EXIST);
        }
        FilesLandOwnership updateFilesLandOwnership = LandOwnershipConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldFilesLandOwnership.get(), updateFilesLandOwnership))
            filesLandOwnershipRepository.update(updateFilesLandOwnership);
        return true;
    }

    @Override
    public LandOwnershipSingleGetOutput singleGet(Long id) {
        Optional<FilesLandOwnership> optionFilesLandOwnership = filesLandOwnershipRepository.findById(id);
        if(!optionFilesLandOwnership.isPresent()){
            throw exception(FILES_LAND_OWNERSHIP_NOT_EXIST);
        }
        return LandOwnershipConvert.INSTANCE.singleGetOutputConvert(optionFilesLandOwnership.get());

    }

}
